import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:rfid/event/wrong_way_event.dart';
import 'package:rfid/http/api.dart';

import '../entity/current_task_entity.dart';
import '../entity/reader_entity.dart';
import '../main.dart';
import '../page/search_book_page.dart';
import '../page/search_door_page.dart';
import '../util/timer_manager.dart';
import 'auth_dialog.dart';
import 'circle_progress_widget.dart';

class DialogWidget{

  doorOpenDialog(no,ReaderEntity readerEntity){
    tts.speak('操作完成后，请关闭柜门');
    timer.startCountDown(CountDownMode.slow,(){
      eventFactory.fire(WrongWayEvent());
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Column(
        children: [
          Container(
            width: 205.w,
            height: 60.h,
            decoration: const BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/person_info.png",
                  ),
                )
            ),
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    left: 30,
                    child: Image.asset("assets/images/head.jpg", width: 30.w, fit: BoxFit.fitWidth,),
                  ),
                  Positioned(
                    left: 40.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(readerEntity.name??'',style: TextStyle(color: Colors.blue,fontWeight: FontWeight.bold,fontSize: 12.sp),),
                        SizedBox(height: 3.h,),
                        Text('读者证号:${readerEntity.no}',style: TextStyle(color: Colors.blue,fontSize: 6.sp)),
                      ],
                    ),
                  ),
                  Positioned(
                      right: 30,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          CircleProgress(size: 36.w,strokeWidth: 2.w,percent: readerEntity.currentBorrowCount!/readerEntity.limitCount!,color: Colors.blue,),
                          Container(
                            width: 30.w,
                            height: 30.w,
                            decoration: BoxDecoration(
                              color: Colors.blue,
                              borderRadius: BorderRadius.all(Radius.circular(25.r)),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text('${readerEntity.currentBorrowCount}/${readerEntity.limitCount}',style: TextStyle(color: Colors.white,fontSize: 6.sp,fontWeight: FontWeight.bold),),
                                Text('借阅数量',style: TextStyle(color: Colors.white,fontSize: 4.sp),)
                              ],
                            ),
                          )
                        ],
                      )
                  ),
                ],
              )
          ),
          SizedBox(height: 5.h,),
          Container(
            width: 205.w,
            height: 260.h,
            decoration: const BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/dialog_bg.png",
                  ),
                )
            ),
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                Positioned(
                    top: 5.h,
                    child: Container(
                      height: 30.h,
                      alignment: Alignment.center,
                      child: Text('$no号格口已开',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                    )
                ),
                Positioned(
                    top: 5.h,
                    right: 10.w,
                    child: Container(
                        height: 30.h,
                        alignment: Alignment.center,
                        child: Obx(() => Text('${timer.slowTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,),)
                    )
                ),
                Positioned(
                    top: 95.h,
                    left: 10.w,
                    right: 10.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Image.asset("assets/images/open_guide.png",height: 90.h, fit: BoxFit.fitHeight,),
                        SizedBox(height: 20.h,),
                        Text('请把书籍朝上放置在指定区域内\n请关闭柜门结算',style: TextStyle(fontSize: 12.sp, color: Colors.red, fontWeight: FontWeight.bold),textAlign: TextAlign.center,)
                      ],
                    )
                ),
              ],
            ),
          )
        ],
      ),
    );
  }


  adminDoorOpenDialog(no){
    tts.speak('操作完成后，请关闭柜门');
    timer.startCountDown(CountDownMode.slow,(){
      eventFactory.fire(WrongWayEvent());
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 205.w,
        height: 260.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/dialog_bg.png",
              ),
            )
        ),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Positioned(
                top: 5.h,
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Text('$no号格口已开',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                )
            ),
            Positioned(
                top: 5.h,
                right: 10.w,
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Obx(() => Text('${timer.slowTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,),)
                )
            ),
            Positioned(
                top: 95.h,
                left: 10.w,
                right: 10.w,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Image.asset("assets/images/open_guide.png",height: 90.h, fit: BoxFit.fitHeight,),
                    SizedBox(height: 20.h,),
                    Text('请关闭柜门结算',style: TextStyle(fontSize: 12.sp, color: Colors.red, fontWeight: FontWeight.bold),textAlign: TextAlign.center,)
                  ],
                )
            ),
          ],
        ),
      ),
    );
  }


  warnOpenDialog(){
    tts.speak('请先关闭柜门');
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 300.w,
        height: 208.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/warn.png",
              ),
            )
        ),
      ),
    );
  }


  String _name = '';
  String _id = '';

  idBindDialog(){
    _name = '';
    _id = '';
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 180.w,
        height: 138.h,
        decoration:BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(8.r)),
        ),
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Positioned(
              top: 8.h,
              right: 12.w,
              child: InkWell(
                onTap: (){
                  timer.cancel();
                  currentTask.clear();
                  Get.until((route) => route.isFirst);
                },
                child: Image.asset("assets/images/close.png",width: 13.w,fit: BoxFit.fitWidth,),
              )
            ),

            Positioned(
              top: 16.h,
              child: Text('身份证绑定',style: TextStyle(color:Colors.blue,fontWeight: FontWeight.bold,fontSize: 12.sp),),
            ),
            Positioned(
              top: 45.h,
              child:  SizedBox(
                width: 140.w,
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration:  InputDecoration(
                    hintText: '请输入真实姓名',
                    hintStyle: TextStyle(color: Colors.grey,fontSize: 6.sp),
                    enabledBorder:  OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
                  ),
                  style: TextStyle(fontSize: 8.sp),
                  onChanged: (value){
                    _name = value;
                  },
                ),
              ),
            ),
            Positioned(
              top: 72.h,
              child:  SizedBox(
                width: 140.w,
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration:  InputDecoration(
                    hintText: '请输入身份证号',
                    hintStyle: TextStyle(color: Colors.grey,fontSize: 6.sp),
                    enabledBorder:  OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
                  ),
                  style: TextStyle(fontSize: 8.sp),
                  onChanged: (value){
                    _id = value;
                  },
                ),
              ),
            ),
            Positioned(
              top: 104.h,
              child: InkWell(
                onTap: () async {
                  var data = await readerApply(currentTask.uid, _name, _id);
                  if(data){
                    Get.back();
                    registerSuccessDialog();
                  }
                },
                child: Image.asset("assets/images/confirm.png",width: 80.w, fit: BoxFit.fitWidth,),
              )
            ),
          ],
        ),
      ),
    );
  }

  borrowOverDialog({bool showOver = false}){
    if(showOver){
      tts.speak('借书已超限，请及时归还');
    }else{
      tts.speak('操作成功');
    }
    timer.startCountDown(CountDownMode.five,(){
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 200.w,
        height: 185.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/borrow_result_bg.jpg",
              ),
            )
        ),
        child: Stack(
          children: [
            Visibility(
              visible: showOver,
              child: Positioned(
                top: 151.h,
                left: 0.w,
                right: 0.w,
                child: Container(width:200.w,alignment:Alignment.center,
                  child: Text('借书量已超限，请及时归还',style: TextStyle(color: Colors.red,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
            ),),
            Positioned(
              right: 28.w,
              top: 2.h,
              child: Container(
                height: 30.h,
                alignment: Alignment.center,
                child: Obx(() => Text('${timer.fiveTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
              )
            ),
            Positioned(
                right: 10.w,
                child: InkWell(
                  onTap: (){
                    timer.cancel();
                    Get.until((route) => route.isFirst);
                  },
                  child: Container(
                      height: 31.h,
                      alignment: Alignment.center,
                      child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                  ),
                )
            ),
          ],
        )
      ),
    );
  }

  registerSuccessDialog(){
    tts.speak('办证成功');
    timer.startCountDown(CountDownMode.five,(){
      currentTask.clear();
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
          width: 200.w,
          height: 254.h,
          decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/register_success.png",
                ),
              )
          ),
          child: Stack(
            children: [
              Positioned(
                  top: 200.h,
                  left: 0.w,
                  right: 0.w,
                  child: InkWell(
                      onTap: (){
                        timer.cancel();
                        currentTask.clear();
                        Get.until((route) => route.isFirst);
                      },
                      child: Container(width:200.w,alignment:Alignment.center,
                        child: Text('读者卡办理成功',style: TextStyle(color: Colors.blue,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
                  )
              ),
              Positioned(
                  right: 28.w,
                  top: 2.h,
                  child: Container(
                    height: 30.h,
                    alignment: Alignment.center,
                    child: Obx(() => Text('${timer.fiveTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
                  )
              ),
              Positioned(
                  right: 10.w,
                  child: InkWell(
                    onTap: (){
                      timer.cancel();
                      currentTask.clear();
                      Get.until((route) => route.isFirst);
                    },
                    child: Container(
                        height: 31.h,
                        alignment: Alignment.center,
                        child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                    ),
                  )
              ),
            ],
          )
      ),
    );
  }

  registerFailedDialog(msg){
    tts.speak("身份证号已注册");
    timer.startCountDown(CountDownMode.five,(){
      currentTask.clear();
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
          width: 200.w,
          height: 254.h,
          decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/register_failed.png",
                ),
              )
          ),
          child: Stack(
            children: [
              Positioned(
                  top: 200.h,
                  left: 0.w,
                  right: 0.w,
                  child: InkWell(
                      onTap: (){
                        timer.cancel();
                        currentTask.clear();
                        Get.until((route) => route.isFirst);
                      },
                      child: Container(width:200.w,alignment:Alignment.center,
                        child: Text('$msg',style: TextStyle(color: Colors.red,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
                  )
              ),
              Positioned(
                  right: 28.w,
                  top: 2.h,
                  child: Container(
                    height: 30.h,
                    alignment: Alignment.center,
                    child: Obx(() => Text('${timer.fiveTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
                  )
              ),
              Positioned(
                  right: 10.w,
                  child: InkWell(
                    onTap: (){
                      timer.cancel();
                      currentTask.clear();
                      Get.until((route) => route.isFirst);
                    },
                    child: Container(
                        height: 31.h,
                        alignment: Alignment.center,
                        child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                    ),
                  )
              ),
            ],
          )
      ),
    );
  }

  borrowTypeDialog(){
    tts.speak('请选择借书方式');
    timer.startCountDown(CountDownMode.fast,(){
      Get.back();
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 205.w,
        height: 260.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/dialog_bg.png",
              ),
            )
        ),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Positioned(
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Text('借书方式',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                )
            ),
            Positioned(
                right: 28.w,
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Obx(() => Text('${timer.fastTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
                )
            ),

            Positioned(
                right: 10.w,
                child: InkWell(
                  onTap: (){
                    timer.cancel();
                    Get.back();
                  },
                  child: Container(
                      height: 30.h,
                      alignment: Alignment.center,
                      child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                  ),
                )
            ),
           Positioned(
              top: 105.h,
              left: 20.w,
              right: 20.w,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    InkWell(
                      child: Image.asset("assets/images/borrow_type_door.png",height: 95.h, width: 76.w, fit: BoxFit.fill,),
                      onTap: (){
                        Get.back();
                        timer.cancel();
                        Get.to(() => const SearchDoor(),arguments: true);
                      },
                    ),
                    InkWell(
                      child: Image.asset("assets/images/borrow_type_book.png",height: 95.h, width: 76.w, fit: BoxFit.fill,),
                      onTap: (){
                        Get.back();
                        timer.cancel();
                        Get.to(() => const SearchBook());
                      },
                    )
                  ],
                ),
            ),
          ],
        ),
      ),
    );
  }

  returnTypeDialog(){
    tts.speak('请选择还书方式');

    timer.startCountDown(CountDownMode.fast,(){
      Get.back();
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 205.w,
        height: 260.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/dialog_bg.png",
              ),
            )
        ),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Positioned(
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Text('还书方式',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                )
            ),
            Positioned(
                right: 28.w,
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Obx(() => Text('${timer.fastTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
                )
            ),

            Positioned(
                right: 10.w,
                child: InkWell(
                  onTap: (){
                    timer.cancel();
                    Get.back();
                  },
                  child: Container(
                      height: 30.h,
                      alignment: Alignment.center,
                      child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                  ),
                )
            ),
            Positioned(
              top: 105.h,
              left: 20.w,
              right: 20.w,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    child: Image.asset("assets/images/return_door.png",height: 95.h, width: 76.w, fit: BoxFit.fill,),
                    onTap: (){
                      Get.back();
                      timer.cancel();
                      if(settingEntity.canReturnIfOver??true){
                        Get.to(() => const SearchDoor(),arguments: false);
                      }else{
                        Get.to(() => const SearchDoor(),arguments: true);
                      }

                    },
                  ),
                  InkWell(
                    child: Image.asset("assets/images/return_fast.png",height: 95.h, width: 76.w, fit: BoxFit.fill,),
                    onTap: (){
                      Get.back();
                      timer.cancel();
                      currentTask.task = Task.borrowWithoutDoor;
                      AuthDialog().authTypeDialog();
                    },
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}